/*
Copyright (c) 2008, Oleg S. Estekhin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the copyright holders nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package ebml;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

/** The default implementation of the <code>ElementDescriptorProvider</code> interface. */
public class DefaultElementDescriptorProvider implements ElementDescriptorProvider {

    private final ElementDescriptor[] descriptors;

    private final Map<VInteger, ElementDescriptor> descriptorsMap;


    /**
     * Creates a new <code>DefaultElementDescriptorProvider</code> object.
     *
     * @param descriptors the array of element descriptors that will be accessible through this provider
     *
     * @throws NullPointerException if <code>descriptors</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>descriptors</code> contains <code>null</code> elements, or elements
     * with invalid identifier, or elements with the same identifier
     */
    public DefaultElementDescriptorProvider(ElementDescriptor[] descriptors) {
        if (descriptors == null) {
            throw new NullPointerException("descriptors is null");
        }
        this.descriptors = descriptors.clone();
        descriptorsMap = new HashMap<VInteger, ElementDescriptor>();
        for (ElementDescriptor elementDescriptor : descriptors) {
            if (elementDescriptor == null) {
                throw new IllegalArgumentException("descriptors contains null");
            }
            if (!elementDescriptor.getIdentifier().isIdentifier()) {
                throw new IllegalArgumentException("descriptors contains elements with invalid identifier");
            }
            if (descriptorsMap.containsKey(elementDescriptor.getIdentifier())) {
                throw new IllegalArgumentException("descriptors contains elements with the same identifier");
            }
            descriptorsMap.put(elementDescriptor.getIdentifier(), elementDescriptor);
        }
    }


    @Override
    public boolean supportsElementIdentifier(VInteger identifier) {
        return descriptorsMap.containsKey(identifier);
    }

    @Override
    public ElementDescriptor getElementDescriptor(VInteger identifier) {
        return descriptorsMap.get(identifier);
    }

    @Override
    public Iterator<ElementDescriptor> iterator() {
        return new ElementDescriptorIterator();
    }


    private final class ElementDescriptorIterator implements Iterator<ElementDescriptor> {

        private int index;

        @Override
        public boolean hasNext() {
            return index < descriptors.length;
        }

        @Override
        public ElementDescriptor next() {
            if (index < descriptors.length) {
                ElementDescriptor descriptor = descriptors[index];
                index++;
                return descriptor;
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

}
